package com.iweipan.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.iweipan.R;
import com.iweipan.adapter.MoneyListAdapter;
import com.iweipan.base.WPBaseFragment;
import com.iweipan.bean.REListBean;
import com.iweipan.utils.sputils.SPUtils;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import okhttp3.Call;

import static com.iweipan.base.WPUrlBase.WP_REWT_LIST;

/**
 * Created by xyb on 2016/12/13.
 */

public class RechargeWithdrawalFragment extends WPBaseFragment {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.error)
    TextView error;
    @BindView(R.id.twinklingRefreshLayout)
    TwinklingRefreshLayout twinklingRefreshLayout;
    private String token;
    private int type;
    private static final int LOADE_MORE = 2;
    private static final int REFRESH = 1;
    private static final int NO_THING = 0;
    private int pageNo = 0;
    private int pagesize = 20;
    private MoneyListAdapter moneyListAdapter;


    public static RechargeWithdrawalFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        RechargeWithdrawalFragment fragment = new RechargeWithdrawalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getInt("type");
        }
        listView.setEmptyView(error);
        moneyListAdapter = new MoneyListAdapter(type, mActivity);
        listView.setAdapter(moneyListAdapter);
        twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                initHttp(REFRESH);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                initHttp(LOADE_MORE);
//                Log.e("RechargeWithdrawalFragm", "LOADE_MORE");
            }
        });
    }

    @Override
    public void initDestrory() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void onResume() {
        super.onResume();
        token = (String) SPUtils.get(mActivity, "wp_token", "空");
        initHttp(NO_THING);
    }

    private void initHttp(final int http_type) {
        JSONObject jsonObject = new JSONObject();

        if (http_type == NO_THING) {
            pageNo = 0;
            pagesize = 20;
        } else if (http_type == LOADE_MORE) {
            pageNo = pagesize + pageNo;
            pagesize = pagesize + 20;
        } else if (http_type == REFRESH) {
            pageNo = 0;
            pagesize = 20;
        }

        try {
            jsonObject.put("token", token);
            if (type == 0) {
                jsonObject.put("type", "re");
            } else if (type == 1) {
                jsonObject.put("type", "wt");
            }
            jsonObject.put("pageNo", pageNo);
            jsonObject.put("pageSize", pagesize);

            OkHttpUtils.get()
                    .url(WP_REWT_LIST)
                    .addParams("p", jsonObject.toString())
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {

                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.e("RechargeWithdrawalFragm", response);
                            try {
                                Gson gson = new Gson();
                                JSONObject rewt = new JSONObject(response);
                                String code = rewt.getString("code");
                                if (code.equals("200")) {
                                    JSONArray result = rewt.getJSONArray("result");
                                    if (result != null && result.length() > 0) {
                                        REListBean reListBean = gson.fromJson(response, REListBean.class);
                                        if (http_type == NO_THING) {
                                            moneyListAdapter.setReListBean(reListBean);
                                        } else if (http_type == LOADE_MORE) {
                                            moneyListAdapter.addReListBean(reListBean);
                                            twinklingRefreshLayout.finishLoadmore();
                                        } else if (http_type == REFRESH) {
                                            moneyListAdapter.setReListBean(reListBean);
                                            twinklingRefreshLayout.finishRefreshing();
                                        }

                                    }else {
                                        twinklingRefreshLayout.finishLoadmore();
                                    }
                                }else {
                                    twinklingRefreshLayout.finishLoadmore();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();

                            }

                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View inflaterView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_rechwith, null);
    }


}
